package cn.com.dhcc.ddi.stat.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.util.dateformatter.DateFormatter;
import cn.com.dhcc.ddi.stat.dao.TaskSyncStatDao;
import cn.com.dhcc.ddi.stat.vo.TaskSyncStatVo;
import cn.com.dhcc.ddi.task.vo.Task;
 /**
 * 
    * @ClassName: DataTaskStatService
    * @Description: 任务同步频次统计Service
    * @author dxy
    * @date 2016-12-13
    *
 */
@Service
public class TaskSyncStatService extends BaseService {
	
	@Autowired
	private TaskSyncStatDao dao;
	
	/**
	 * 每日统计
	 * @throws ServiceException
	 */
	public void dailyTaskStatistics() throws ServiceException {
		try {
			List<TaskSyncStatVo> stats = dao.getDailyTaskStatistics();
			for(TaskSyncStatVo vo: stats) {
				vo.setID(UUIDGenerator.getUUID());
				vo.setMtime(new Date());
				vo.setStatDate(DateFormatter.getDateBasedOnTodayForDate("yy/MM/dd", -1));
				insert(vo);
			}
		} catch (DaoException e) {
			throw new ServiceException("根据任务统计失败", e);
		}
		
	}
	
	/**
	 * 根据任务的统计数据查询
	 * @param scope 0-天，1-月，2-年
	 * @return
	 * @throws ServiceException 
	 * @throws DaoException 
	 */
	public List<TaskSyncStatVo> getTaskStatData(WhereCondition taskParameter,int scope, String date) throws ServiceException {
		try {	
			String start = "";
			String end = "";
			if(scope == 0) {
				start = date + " 00:00:00";
				end = date + " 23:59:59";
			} else if(scope == 1) {
				start = date + "-01 00:00:00";
				end = date + "-31 23:59:59";
			} else {
				start = date + "-01-01 00:00:00";
				end = date + "-12-31 23:59:59";
			}
			return dao.getTaskStatData(taskParameter,start, end);
		} catch (DaoException e) {
			throw new ServiceException("根据任务获取统计数据失败", e);
		}
	}
	
	/**
	 * 获取任务统计分页数据
	 * @param pager
	 * @param condition
	 * @param scope
	 * @param date
	 * @return
	 * @throws ServiceException
	 */
	public PagerData<TaskSyncStatVo> getTaskStatPagerData(Pager pager, WhereCondition condition, int scope, String date) throws ServiceException {
		try {	
			String start = "";
			String end = "";
			if(scope == 0) {
				start = date + " 00:00:00";
				end = date + " 23:59:59";
			} else if(scope == 1) {
				start = date + "-01 00:00:00";
				end = date + "-31 23:59:59";
			} else {
				start = date + "-01-01 00:00:00";
				end = date + "-12-31 23:59:59";
			}
			
			return dao.getTaskStatPagerData(pager, condition, start, end);
		} catch (DaoException e) {
			throw new ServiceException("根据任务获取统计数据失败", e);
		}
	}
	
	public List<TaskSyncStatVo> getCalendarData(String start, String end, String taskId,WhereCondition condition) throws ServiceException {
		try {	
			return dao.getCalendarData(start, end, taskId,condition);
		} catch (DaoException e) {
			throw new ServiceException("根据任务获取日历数据失败", e);
		}
	}
	
	public PagerData<TaskSyncStatVo> getCalendarPagerData(Pager pager, WhereCondition condition, String date, String taskId) throws ServiceException {
		try {	
			String start = date + " 00:00:00";
			String end = date + " 23:59:59";
			return dao.getCalendarPagerData(pager, condition, start, end, taskId);
		} catch (DaoException e) {
			throw new ServiceException("根据任务获取日历分页数据失败", e);
		}
	}
	
	public List<TaskSyncStatVo> getNodeStatData(String type, String date, String cataId, int scope) throws ServiceException {
		try {	
			String start = "";
			String end = "";
			if(scope == 0) {
				start = date + " 00:00:00";
				end = date + " 23:59:59";
			} else if(scope == 1) {
				start = date + "-01";
				end = date + "-31";
			} else {
				start = date + "-01-01";
				end = date + "-12-31";
			}
			return dao.getNodeStatData(type, start, end, cataId, null);
		} catch (DaoException e) {
			throw new ServiceException("根据节点获取统计数据失败", e);
		}
	}
	
	
	
	
	
	
	
	/**
	 * 获取24小时数据量统计数据
	 * @param id 任务id
	 * @return 
	 * @throws ServiceException
	 */
	public List<String> getSynTaskByHour(String id) throws ServiceException {
		List<Task> list;
		try {
			list = dao.getSynTasksByHour(id);
			
			List<String> counts = new ArrayList<String>();
			for(int i = 0; i <= 23; i++) {
				counts.add(null);
			}
			
			for(Task info: list) {
				int index = Integer.parseInt(info.getHour());
				counts.set(index, info.getSum());
			}
			
			return counts;
		} catch (DaoException e) {
			throw new ServiceException("获取24小时数据量统计", e);
		}
		
	}
	/**
	 * 获取24小时频次统计数据
	 * @param id 任务id
	 * @return 
	 * @throws ServiceException
	 */
	public List<String> getSynTotalByHour(String id) throws ServiceException {
		List<Task> list;
		try {
			list = dao.getSynTotalByHour(id);
			
			List<String> counts = new ArrayList<String>();
			for(int i = 0; i <= 23; i++) {
				counts.add(null);
			}
			
			for(Task info: list) {
				int index = Integer.parseInt(info.getHour());
				counts.set(index, info.getCount());
			}
			
			return counts;
		} catch (DaoException e) {
			throw new ServiceException("获取24小时频次统计", e);
		}
		
	}
	
	/**
	 * 获取所有任务 
	 * @return
	 * @throws ServiceException
	 */
	public List<Task> getTasks() throws ServiceException {
		try {
			List<Task> tasks = dao.getTasks();
			return tasks;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部任务", e);
		}	
		
	}
	
	/**
	 * 按顺序获取任务名称
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getTaskNames() throws ServiceException {
		try {
			List<Task> tasks = dao.getTasks();
			List<String> names = new ArrayList<String>();
			for(Task task : tasks) {
				names.add(task.getTaskName());
			}
			return names;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部任务名称", e);
		}
	}
	
	/**
	 * 按顺序获取任务id
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getTaskId() throws ServiceException {
		try {
			List<Task> tasks = dao.getTasks();
			List<String> ids = new ArrayList<String>();
			for(Task task : tasks) {
				ids.add(task.getId());
			}
			return ids;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部任务id", e);
		}
	}
	
	/**
	 * 获取所有任务数据量统计数据（按天，月或者年）
	 * @param scope 0-天，1-月，2-年
	 * @param date 日期
	 * @return
	 * @throws ServiceException
	 */
	public Map<String, String> getSynCount(int scope, String date) throws ServiceException {
		Map<String, String> data = new HashMap<String, String>();
		
		switch(scope) {
		case 0:
			data = getSynDatasByDay(date);
			break;
			
		case 1:
			data = getSynDatasByMonth(date);
			break;
			
		case 2:
			data = getSynDatasByYear(date);
			break;
			
		default:
			data = getSynDatasByDay(date);
			break;
		}
		
		return data;
		
		
	}
	
	/**
	 * 按天获取数据量统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getSynDatasByDay(String date) throws ServiceException {
		try {
			String start = date + " 00:00:00";
			String end = date + " 23:59:59";
			Map<String, String> data = new HashMap<String, String>();
			List<TaskSyncStatVo> synVos = dao.getTotalInfo(start, end);
			
			for(TaskSyncStatVo vo: synVos) {
				data.put(vo.getTaskId(), "" + vo.getSum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	
	/**
	 * 按月获取数据量统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getSynDatasByMonth(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01";
			String end = date + "-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<TaskSyncStatVo> synVos = dao.getTotalInfo(start, end);
			
			for(TaskSyncStatVo vo: synVos) {
				data.put(vo.getTaskId(), "" + vo.getSum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	
	/**
	 * 按年获取数据量统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getSynDatasByYear(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01-01";
			String end = date + "-12-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<TaskSyncStatVo> syndVos = dao.getTotalInfo(start, end);
			
			for(TaskSyncStatVo vo: syndVos) {
				data.put(vo.getTaskId(), "" + vo.getSum());
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	/**
	 * 获取所有任务频次统计数据（按天，月或者年）
	 * @param scope 0-天，1-月，2-年
	 * @param date 日期
	 * @return
	 * @throws ServiceException
	 */
	public Map<String, String> getSynTotalCount(int scope, String date) throws ServiceException {
		Map<String, String> data = new HashMap<String, String>();
		
		switch(scope) {
		case 0:
			data = getSynTotalByDay(date);
			break;
			
		case 1:
			data = getSynTotalByMonth(date);
			break;
			
		case 2:
			data = getSynTotalByYear(date);
			break;
			
		default:
			data = getSynTotalByDay(date);
			break;
		}
		
		return data;
		
		
	}
	/**
	 * 按天获取频次统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getSynTotalByDay(String date) throws ServiceException {
		try {
			String start = date + " 00:00:00";
			String end = date + " 23:59:59";
			Map<String, String> data = new HashMap<String, String>();
			List<TaskSyncStatVo> synVos = dao.getTotalInfo(start, end);
			
			for(TaskSyncStatVo vo: synVos) {
				data.put(vo.getTaskId(), "" + vo.getNum());
			
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	
	/**
	 * 按月获取频次统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getSynTotalByMonth(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01";
			String end = date + "-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<TaskSyncStatVo> synVos = dao.getTotalInfo(start, end);
			
			for(TaskSyncStatVo vo: synVos) {
				data.put(vo.getTaskId(), "" + vo.getNum());
			
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	
	/**
	 * 按年获取频次统计数据
	 * @param date
	 * @return 
	 * @throws ServiceException
	 */
	public Map<String, String> getSynTotalByYear(String date) throws ServiceException {
		try {
			//修改日期字符串
			String start = date + "-01-01";
			String end = date + "-12-31";
			
			Map<String, String> data = new HashMap<String, String>();
			List<TaskSyncStatVo> synVos = dao.getTotalInfo(start, end);
			
			for(TaskSyncStatVo vo: synVos) {
				data.put(vo.getTaskId(), "" + vo.getNum());
				
			}
			
			return data;
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
	}
	
	/**
	 * 执行每日统计，并放入数据库
	 * @throws ServiceException
	 */
	public void performDailySynStatics() throws ServiceException {
		
		List<TaskSyncStatVo> synTask = new ArrayList<TaskSyncStatVo> ();
		
		try {
			synTask = dao.CalculateDailySynStatistics();
			
		} catch (DaoException e) {
			throw new ServiceException("获取全部任务", e);
		}
		
		for(TaskSyncStatVo vo: synTask) {
			
			insert(updateDataSynStatVo(vo));
			
		}
	
	}
	
	/**
	 * 更新统计实例
	 * @param 原有实例
	 * @return
	 * @throws ServiceException
	 */
	public TaskSyncStatVo updateDataSynStatVo(TaskSyncStatVo vo) throws ServiceException {
		vo.setTotal(Integer.parseInt(vo.getSum()));
		vo.setDataSize(Integer.parseInt(vo.getNum()));
		vo.setID(UUIDGenerator.getUUID());
		vo.setStatDate(DateFormatter.getDateBasedOnTodayForDate("yy/MM/dd", -1));
		vo.setMtime(DateFormatter.getTodayForDate("yyyy-MM-dd HH:mm:ss"));
		
		return vo;
	}
	
	
	/**
	 * 获取列表
	 * @param pager
	 * @param condition
	 * @return
	 * @throws ServiceException
	 */
	public PagerData<TaskSyncStatVo> list(Pager pager, WhereCondition condition) throws ServiceException {
		try {
			return dao.list(pager, condition);
		} catch (DaoException e) {
			throw new ServiceException("获取列表失败", e);
		}
	}
	
	/**
	 * 更新
	    * @Title: insert
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param vo
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return int    返回类型
	    * @throws
	 */
	public int insert(TaskSyncStatVo vo) throws ServiceException {
			
			try {
				return dao.insert(vo);
			} catch (DaoException e)  {
				throw new ServiceException("新增失败", e);
			}
		}
	  public int update(TaskSyncStatVo vo) throws ServiceException {
			
			try {
				return dao.update(vo);
			}catch (DaoException e)  {
				throw new ServiceException("更新失败", e);
			}
		}
	
}
